package leetcode.code2191;

import java.util.Arrays;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public int[] sortJumbled(int[] mapping, int[] nums) {
		int len = nums.length;
		int[][] tmp = new int[len][3];
		for (int i = 0; i < len; i++) {
			int cur = nums[i];
			tmp[i][0] = cur;
			tmp[i][1] = map(cur, mapping);
			tmp[i][2] = i;
		}
		Arrays.sort(tmp, (a, b) -> a[1] == b[1] ? a[2] - b[2] : a[1] - b[1]);
		int[] ans = new int[len];
		for (int i = 0; i < len; i++) {
			ans[i] = tmp[i][0];
		}
		return ans;
	}

	private int map(int cur, int[] mapping) {
		if (cur == 0) {
			return mapping[0];
		}
		int basic = 1;
		int ans = 0;
		while (cur > 0) {
			int last = cur % 10;
			ans += basic * mapping[last];
			basic *= 10;
			cur /= 10;
		}
		return ans;
	}

	@Override
	public void debug1() {
		int[] ans = HelpDebug.str2array3("[9,8,7,6,5,4,3,2,1,0]");
		HelpDebug.compare(ans, this.sortJumbled(HelpDebug.str2array3("[9,8,7,6,5,4,3,2,1,0]"), HelpDebug.str2array3("[0,1,2,3,4,5,6,7,8,9]")));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
